/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.systray;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;
import com.mobiwebinc.compconn.gui.About;
import com.mobiwebinc.compconn.gui.ConfigurationGUI;
import com.mobiwebinc.compconn.gui.MainMenu;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author suraj
 */
public class MacTray extends SysTray {

    JMenuBar menuBar;

    public MacTray() {
        init();
    }

    @Override
    public void init() {
        macSetup();
        menuBar = new JMenuBar();
        Application app = Application.getApplication();
        app.setDockMenu(MainMenu.getMainMenu().getPopupMenu());
        app.setEnabledPreferencesMenu(true);
        app.setEnabledAboutMenu(true);
        app.addApplicationListener(new ApplicationAdapter() {

            @Override
            public void handleAbout(ApplicationEvent event) {
                event.setHandled(true);
                About.showDialog();
            }

            @Override
            public void handlePreferences(ApplicationEvent e) {
                e.setHandled(true);
                ConfigurationGUI.getInstance().showDialog();
            }

            @Override
            public void handleQuit(ApplicationEvent e) {
                e.setHandled(true);
                System.exit(0);
            }
        });
        menuBar.add(MainMenu.getMainMenu().getMenu());
        ConfigurationGUI.getInstance().setJMenuBar(menuBar);
    }

    private void macSetup() {
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MobiDesk Server");
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MacTray.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MacTray.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MacTray.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MacTray.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
